﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YGame.Base.Part;
using YGame.Base;

namespace YGame.GameThing.Part
{
	public class PackageException : Exception
	{
		public PackageType PackageType { get; set; }
		public int Pos { get; set; }
		public IGoods Goods { get; set; }
		public string Message { get; set; }
	}

	public class PackagePart: BasePart, IPackagePart
	{
		IGoods[][] _Container;
		IGoods[] _normalContainer;
        IGoods[] _taskContainer;
		int[] _containerSize;

		public PackagePart()
		{
			_containerSize= new int [(int)PackageType.Max];
			_Container = new IGoods[(int)PackageType.Max][];
			_Container[(int)PackageType.Normal] = new IGoods[(int)PackageTypeSize.Normal];
			_Container[(int)PackageType.Task] = new IGoods[(int)PackageTypeSize.Task];

		}

		#region IBasePart 成员
		public void Load()
		{
			
		}
		public void Save()
		{
			

		}
		#endregion

		#region IPackagePart 成员

		public int GetSize(PackageType packageType)
		{
			return _containerSize[(int)packageType];
		}

		public int SetSize(PackageType packageType, int newSize)
		{
			_containerSize[(int)packageType] = newSize;
			return newSize;
		}

		public int GetGoodsCount(PackageType packageType, int goodsID = 0, Base.GoodsFlag flag = GoodsFlag.Ignore)
		{
			throw new NotImplementedException();
		}


		public Base.IGoods GetGoods(PackageType packageType, int pos)
		{
			return _Container[(int)packageType][pos];
		}

		public int AddGoods(PackageType packageType, IGoods goods, int pos)
		{
			if (pos==-1)
			{
				pos = GetFreePos(packageType);
			}

			IGoods[] container =  _Container[(int)packageType];
			if (container[pos] != null)
			{
				throw new PackageException() { PackageType = packageType, Pos = pos, Goods = goods, Message = "已经存在" };
			}
			container[pos] = goods;
			return pos;
		}

		private int GetFreePos(PackageType packageType)
		{
			IGoods[] container = _Container[(int)packageType];
			for (int i = 0; i < container.Length; i++)
			{
				if (container[i]==null)
				{
					return i;
				}
			}

			throw new PackageException() { PackageType = packageType, Message = "没位置" };
		}

		public int RemoveGoods(PackageType packageType, IGoods goods)
		{
			throw new NotImplementedException();
		}

		public Base.IGoods RemoveGoods(PackageType packageType, int pos)
		{
			throw new NotImplementedException();
		}

		#endregion
	}
}
